package com.leilei.serviceProject.domain.strategy.service.rule.tree.impl;

import com.leilei.serviceProject.domain.strategy.model.valobj.RaffleResultCheckVo;
import com.leilei.serviceProject.domain.strategy.service.rule.tree.ILogicTreeNode;
import com.leilei.serviceProject.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author yanglei
 * @description 规则树奖品次数锁节点
 * @create 2024/11/19 17:14
 */
@Slf4j
@Component("rule_lock")
public class RuleLockLogicTreeNode implements ILogicTreeNode {
    private Long userRaffleCount = 10L;


    // 判断用户抽奖次数和奖品解锁次数的大小：用户抽奖次数 >= 奖品解锁次数放行走下一个规则树节点（库存扣减）。用户抽奖次数 < 奖品解锁次数拦截走下一个规则树节点（兜底）
    @Override
    public DefaultTreeFactory.TreeLogicResultEntity logic(Long strategyId, String userId, Integer awardId, String ruleValue) {
        log.info("规则树过滤-次数锁 userId:{} strategyId:{} awardId:{} ruleValue:{}", userId, strategyId, awardId, ruleValue);
        Long raffleCount = 0L;
        try {
            raffleCount = Long.parseLong(ruleValue);
        } catch (Exception e) {
            throw new RuntimeException("规则树过滤-次数锁异常 ruleValue: " + ruleValue + " 配置不正确");
        }
        // 放行：用户抽奖次数 >= 数据库配置次数
        if (userRaffleCount > raffleCount) {
            return DefaultTreeFactory.TreeLogicResultEntity.builder()
                    .ruleTreeResultCheck(RaffleResultCheckVo.ALLOW)
                    .build();
        }
        // 阻断：用户抽奖次数 < 数据库配置次数
        return DefaultTreeFactory.TreeLogicResultEntity.builder()
                .ruleTreeResultCheck(RaffleResultCheckVo.TAKE_OVER)
                .build();
    }
}
